let requestUrl = null;
/**
 * 空白图片的URL地址
 */
let url = "/images/blank.png";
/**
 * 子女装备类型
 */
let selectorArray = [null, "#sz", "#dz", "#jh"];
/**
 * 子女装备操作
 */
let operationArray = ["&emsp;&emsp;穿戴&emsp;&emsp;", "&emsp;&emsp;取下&emsp;&emsp;"];

/**
 * 子女装备类型
 */
let equipmentTypes = [null, {
	type: 1,
	name: "手镯",
	selector: "#sz"
}, {
	type: 2,
	name: "吊坠",
	selector: "#dz"
}, {
	type: 3,
	name: "脚环",
	selector: "#jh"
}];

function addHandler() {
	let roleId = $("#role_id").combobox("getValue");
	let schoolId = $("#school_id").combobox("getValue");
	let categoryId = $("#category_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}
	if (schoolId) {
		$("#schoolId").combobox("setValue", schoolId);
	}
	if (categoryId) {
		$("#categoryId").combobox("setValue", categoryId);
	}

	requestUrl = "/children/insert";

	$("#children_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#children_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/children/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#schoolId").combobox("setValue", rowData.schoolId);
		$("#categoryId").combobox("setValue", rowData.categoryId);
		$("#healthiness").numberspinner("setValue", rowData.healthiness);
		$("#improveTimes").numberspinner("setValue", rowData.improveTimes);

		$("#children_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#children_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/children/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#children_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的子女！", "warning");
	}
}

function zizhiHandler() {
	let rowData = $("#children_list").datagrid("getSelected");

	if (rowData) {
		let childrenId = rowData.id;

		$("#childrenId").val(childrenId);

		ajaxGet("/children_zizhi/selectByChildrenId", {
			childrenId: childrenId
		}, function(resp) {
			let data = resp.data;

			$("#zizhi_gjzz").numberspinner("setValue", data.gjzz);
			$("#zizhi_fyzz").numberspinner("setValue", data.fyzz);
			$("#zizhi_tlzz").numberspinner("setValue", data.tlzz);
			$("#zizhi_flzz").numberspinner("setValue", data.flzz);
			$("#zizhi_sdzz").numberspinner("setValue", data.sdzz);
			$("#zizhi_chengzhang").numberspinner("setValue", data.chengzhang);
		}, error);

		requestUrl = "/children_zizhi/updateByChildrenId";

		$("#zizhi_dialog").dialog("open");
	} else {
		unselected();
	}
}

function viewHandler() {
	let rowData = $("#children_list").datagrid("getSelected");

	if (rowData) {
		let childrenId = rowData.id;

		$("#children_datalist").datalist("reload", {
			fallback: function () {
				$("#children_datalist").datalist("selectRecord", childrenId);
			}
		});
		$("#children_detail").dialog("open");
	} else {
		unselected();
	}
}

/**
 * 加载子女资质
 */
function loadZizhi() {
	let rowData = $("#children_datalist").datalist("getSelected");

	if (rowData) {
		let childrenId = rowData.id;

		ajaxGet("/children_zizhi/selectByChildrenId", {
			childrenId: childrenId
		}, function (resp) {
			let data = resp.data;

			let gjzz = data.gjzz;
			let fyzz = data.fyzz;
			let tlzz = data.tlzz;
			let flzz = data.flzz;
			let sdzz = data.sdzz;

			$("#children_gjzz").progressbar("setValue", gjzz);
			$("#children_fyzz").progressbar("setValue", fyzz);
			$("#children_tlzz").progressbar("setValue", tlzz);
			$("#children_flzz").progressbar("setValue", flzz);
			$("#children_sdzz").progressbar("setValue", sdzz);
			// 成长显示为小数
			$("#children_chengzhang").html((data.chengzhang / 1000).toFixed(3));

			// 查询对应子女类型的预览资质
			ajaxGet("/children_view_zizhi/selectByChildrenId", {
				childrenId: childrenId
			}, function (resp) {
				let viewData = resp.data;

				if(viewData) {
					// 子女普通资质上限
					$("#children_gjzz").progressbar("setPercent", gjzz * 100 / viewData.gjzz);
					$("#children_fyzz").progressbar("setPercent", fyzz * 100 / viewData.fyzz);
					$("#children_tlzz").progressbar("setPercent", tlzz * 100 / viewData.tlzz);
					$("#children_flzz").progressbar("setPercent", flzz * 100 / viewData.flzz);
					$("#children_sdzz").progressbar("setPercent", sdzz * 100 / viewData.sdzz);
				}
			}, error);
		}, error);
	}
}

/**
 * 加载子女技能
 */
function loadSkills() {
	let rowData = $("#children_list").datagrid("getSelected");

	if (rowData) {
		let childrenId = rowData.id;

		// 清空技能列表
		$("#children_skills").empty();
		console.log("empty()...");

		// 加载子女技能列表
		ajaxGet("/children_skill/selectByChildrenId", {
			childrenId: childrenId
		}, function (resp) {
			let total = 0;
			let list = resp.data;
			let children_skills = "<tr>";

			if (list && list.length > 0) {
				for(let i = 0; i < list.length; i++) {
					//
					// let cd = list[i].cd;
					let name = list[i].name;
					let note = list[i].note;
					let image = list[i].image;

					children_skills += "<td>";

					let title = name + "：" + note;

					children_skills += "<div class='skill_box' title='" + title + "'>";
					children_skills += "<img src='" + image + "' />";

					children_skills += "</div>";
					children_skills += "</td>";

					total ++;

					if(total % 4 === 0 && (i !== length - 1)) { // 4个技能1行
						children_skills += "</tr><tr>";
					}
				}

				$("#children_skills").append(children_skills);
				$("#children_detail").dialog("open");
			}
		}, error);
	} else {
		unselected();
	}
}

/**
 * 加载子女装备
 */
function loadEquipments() {
	for (let i = 0; i < selectorArray.length; i++) {
		$(selectorArray[i]).attr("src", url).attr("ref", "");
	}

	let rowData = $("#children_datalist").datalist("getSelected");

	if (rowData) {
		let childrenId = rowData.id;

		/**
		 * 查询子女已穿戴的子女装备
		 */
		ajaxGet("/children_equipment/getWeardEquipments", {
			childrenId: childrenId
		}, function (resp) {
			let list = resp.data;

			if (list.length > 0) {
				for (let i = 0; i < list.length; i++) {
					let data = list[i];
					let type = data.type;

					$(selectorArray[type]).attr({
						src: data.image,
						alt: data.name,
						ref: data.id
					});
				}
			}
		}, error);
	}
}

/**
 * 点击子女装备格子
 * @param _this
 * @param type 子女装备类型
 */
function clickEquipment(_this, type) {
	$("#hw").parent("div").css("border", "0");
	$("#xq").parent("div").css("border", "0");
	$("#kj").parent("div").css("border", "0");

	_this.parent("div").css("border", "1px blue solid");

	let rowData = $("#children_datalist").datalist("getSelected");

	if (rowData) {
		equipmentId = _this.attr("ref");

		if (equipmentId) {
			$("#equipment_list").datalist("reload", {
				type: type,
				childrenId: rowData.id,
				callback: function () {
					$("#equipment_list").datalist("selectRecord", equipmentId);
				}
			});
		} else {
			$("#equipment_list").datalist("reload", {
				type: type
			});

			$("#equipment_dialog").dialog("open");
		}
	}
}

function prepare() {
	// 子女装备属性
	$("#property_white").empty();
	$("#property_green").empty().hide();
	$("#property_blue").empty().hide();
	// 星石属性
	$("#xingshi").hide();
	$("#property_xingshi_base").empty().hide();
	$("#property_xingshi_fuling").empty().hide();
	$("#property_xianghun").empty().hide();
}

/**
 * 查看子女装备详情
 */
function viewEquipment(rowData) {
	prepare();

	// 子女装备详情——基本信息
	ajaxGet("/children_equipment_category/selectById", {
		id: rowData.categoryId
	}, function(resp) {
		let data = resp.data;

		$("#equipment_name").html(data.name);
		$("#equipment_note").html(data.note);
		$("#equipment_grade").html(data.grade);
		$("#equipment_image").attr("src", data.image);
		$("#equipment_type").html(equipmentTypes[data.type].name);
	}, error);

	// 查询子女装备属性
	ajaxGet("/children_equipment_attribute/selectAttributes", {
		equipmentId: rowData.id
	}, function(resp) {
		let data = resp.data;

		if(data.length > 0) {
			$("#property").show();

			for(let i = 0; i < data.length; i++) {
				let attribute = data[i];
				let property =  "&emsp;" + attribute.propertyName + "&ensp;+" + attribute.propertyValue;

				if (attribute.propertyType === 1) { // 1、基础属性
					// 只有一条基础属性
					$("#property_white").empty().append(property);
				} else if (attribute.propertyType === 2) { // 2、附加属性
					$("#property_green").append(property).show();
				}
			}
		}
	}, error);

	/**
	 * 查询子女装备镶嵌的星石
	 */
	ajaxGet("/xingshi/selectByEquipmentId", {
		equipmentId: rowData.id
	}, function(resp) {
		let data = resp.data;

		if(data) {
			$("#xingshi").show();

			let xingshiId = data.id;

			/**
			 * 查询星石属性
			 */
			ajaxGet("/xingshi/selectAttribute", {
				xingshiId: xingshiId
			}, function(resp) {
				let attribute = resp.data;

				if (attribute) {
					let property = "&emsp;" + data.grade + "级" + attribute.name;

					property += "&ensp;" + attribute.propertyName + "&ensp;+" + attribute.propertyValue;

					$("#property_xingshi_base").append(property).show();
				}
			}, error);

			/**
			 * 查询星石的附灵信息
			 */
			ajaxGet("/xingshi_fuling/selectAttributes", {
				xingshiId: xingshiId
			}, function(resp) {
				let data = resp.data;

				if (data && data.length > 0) {
					let content = "";

					for (let i = 0; i < data.length; i++) {
						content += "&emsp;" + data[i].propertyName + "&ensp;+" + data[i].propertyValue;
					}

					$("#property_xingshi_fuling").append(content).show();
				}
			}, error);

			/**
			 * 查询星石的相魂属性
			 */
			ajaxGet("/xingshi_xianghun/selectByXingshiId", {
				xingshiId: xingshiId
			}, function(resp) {
				let data = resp.data;

				if(data) {
					$("#property_xingshi_xianghun").html("&emsp;" + data.effect).show();
				}
			}, error);
		}
	}, error);

	let skillId = rowData.skillId;

	if(skillId) {
		$("#property_blue").show();

		ajaxGet("/chongwu_skill_category/selectById", {
			id: skillId
		}, function(resp) {
			let data = resp.data;

			$("#property_blue").append("<div class='blue'>&emsp;技能&ensp;" + data.name + "</div>");
		}, error);
	}

	$("#equipment_forger").html(rowData.forger);

	$("#wear").linkbutton({
		text: operationArray[rowData.wearStatus]
	});

	$("#equipment_detail").dialog("open");
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择角色-",
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_modeling_" + row.id).attr({
					alt: data.name,
					src: data.image,
					title: data.name
				});
			}, error);

			return "<img height='18' id='role_modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#school_id").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择门派-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#category_id").combobox({
		url: "/children_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择造型-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let name = $("#_name_").textbox("getValue");
		let type = $("#_type_").numberspinner("getValue");
		let schoolId = $("#school_id").combobox("getValue");

		$("#children_list").datagrid("reload", {
			schoolId: schoolId,
			name: name,
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#grade").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#healthiness").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#improveTimes").numberspinner({
		min: 0,
		max: 3,
		width: 150,
		required: true
	});

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_" + row.id).attr({
					alt: data.name,
					src: data.image,
					title: data.name
				});
			}, error);

			return "<img height='18' id='role_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	fetchSchoolId();

	$("#categoryId").combobox({
		url: "/children_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
		}
	});

	$("#children_dialog").dialog({
		title: "子女信息",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#children_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#children_dialog").dialog("close");
						$("#children_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#children_form").form("clear");
				$("#children_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	$("#zizhi_gjzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#zizhi_fyzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#zizhi_tlzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#zizhi_flzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#zizhi_sdzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#zizhi_chengzhang").numberspinner({
		min: 0,
		max: 1300,
		width: 120,
		required: true
	});

	$("#zizhi_dialog").dialog({
		title: "子女资质",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#zizhi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#zizhi_dialog").dialog("close");
						$("#children_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#zizhi_form").form("clear");
				$("#zizhi_dialog").dialog("close");
			}
		}]
	});
	
	/******************************************************************************************************/

	$("#jiadian_qixue").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_wlsh").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_fssh").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_wlfy").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_fsfy").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_sudu").numberspinner({
		min: 0,
		width: 100,
		editable: true,
		required: true
	});

	$("#jiadian_help").linkbutton({
		iconCls: "icon-help",
		onClick: function() {
			let offest = $(this).offset();

			$("#jiadian_note").dialog("open").dialog("move", {
				left: offest.left - 90,
				top: offest.top - 160
			});
		}
	});

	$("#jiadian_tizhi").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

	$("#jiadian_moli").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

	$("#jiadian_liliang").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

	$("#jiadian_naili").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

	$("#jiadian_minjie").numberspinner({
		min: 0,
		width: 120,
		required: true
	});

	$("#reset").linkbutton({
		text: "重置属性"
	}).click(function() {
		unopen();
	});

	$("#save").linkbutton({
		text: "确认加点"
	}).click(function() {
		let rowData = $("#children_datalist").datalist("getSelected");

		if (rowData) {
			let childrenId = rowData.id;

			$("#children_id").val(childrenId);
		}

		let selector = "#jiadian_form";

		checkForm(selector, function () {
			let data = $(selector).serialize();

			ajaxPost("/children_jiadian/update", data, function(resp) {
				showMsg(resp.message);

				$(selector).form("clear");
				$("#jiadian_dialog").dialog("close");
			}, error);
		});
	});

	/**
	 * 子女加点对话框
	 */
	$("#jiadian_dialog").dialog({
		title: "子女加点"
	});

	/******************************************************************************************************/

	$("#jiadian_note > div > .splitter").textbox({
		width: 300
	});

	$("#jiadian_note").dialog({
		title: " ",
		modal: false,
		closed: true,
		closable: true,
		draggable: false
	});

	/******************************************************************************************************/

	$("#children_datalist").datalist({
		url: "/children/selectAll",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 190,
		height: 195,
		textFormatter: function(value, rowData, rowIndex) {
			ajaxGet("/children_category/selectById", {
				id: rowData.categoryId
			}, function(res) {
				let data = res.data;

				$("#name_" +  rowIndex).html(rowData.name);
				$("#grade_" +  rowIndex).html(rowData.grade);
				$("#children_category_" +  rowIndex).attr({
					alt: data.name,
					src: data.image,
					title: data.name
				});
			}, error);

			let html = "";

			html += "<table>";
			html += 	"<tr>";
			html += 		"<td rowspan=2>";
			html += 			"<div style='border:1px black solid;width:54px;height:54px;'>";
			html +=					"<img width='52' id='children_category_" +  rowIndex + "' />";
			html +=				"</div>";
			html += 		"</td>";
			html += 	"</tr>";

			html += 	"<tr>";
			html += 		"<td>";
			html += 			"<div>";
			html +=					"<div id='name_" +  rowIndex + "'></div>";
			html +=					"<span id='grade_" +  rowIndex + "'></span>" + "级";
			html += 			"</div>";
			html += 		"</td>";
			html += 	"</tr>";
			html += "</table>";

			return html;
		},
		onSelect: function(rowIndex, rowData) {
			if (rowData) {
				// 左侧面板内容
				$("#children_name").html(rowData.name);
				$("#children_grade").html(rowData.grade);

				ajaxGet("/children_category/selectById", {
					id: rowData.categoryId
				}, function (resp) {
					let data = resp.data;

					$("#children_image").prop("src", data.image);
				}, error);

				/*右侧面板内容*/
				// 属性装备
				ajaxGet("/children_attribute/selectByChildrenId", {
					childrenId: rowData.id
				}, function (resp) {
					let data = resp.data;

					// 设置气血、魔法、经验进度条的值
					$("#qixue").progressbar("setText", "{value}/" + data.qixue);
					$("#exp").progressbar("setText", "{value}/" + data.exp);

					$('#qixue').progressbar("setValue", data.qixue);
					$('#exp').progressbar("setValue", data.exp);

					// 设置子女属性
					$("#children_wlsh").html(data.wlsh);
					$("#children_wlfy").html(data.wlfy);
					$("#children_fssh").html(data.fssh);
					$("#children_fsfy").html(data.fsfy);
					$("#children_sudu").html(data.sudu);
				}, error);

				loadZizhi();
				loadSkills();
				loadEquipments();


				// 设置健康度、亲密度进度条的值
				$("#health").progressbar("setText", "{value}/100")
					.progressbar("setValue", rowData.healthiness)
					.progressbar("setPercent", rowData.healthiness);
				$("#intimacy").progressbar("setText", "{value}/100")
					.progressbar("setValue", 78)
					.progressbar("setPercent", 78);

				$("#children_detail").dialog("open");
			}
		}
	});

	/**
	 * 气血
	 */
	$("#qixue").progressbar({
		width: 240,
		text: "{value}"
	}).progressbar("setColor", "red");

	/**
	 * 经验
	 */
	$("#exp").progressbar({
		width: 200,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 攻击资质
	 */
	$("#children_gjzz").progressbar({
		width: 90,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 防御资质
	 */
	$("#children_fyzz").progressbar({
		width: 90,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 体力资质
	 */
	$("#children_tlzz").progressbar({
		width: 90,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 法力资质
	 */
	$("#children_flzz").progressbar({
		width: 90,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 速度资质
	 */
	$("#children_sdzz").progressbar({
		width: 90,
		text: "{value}"
	}).progressbar("setColor", "green");

	$("#set_jiadian").linkbutton({
		text: "属性点分配",
		onClick: function () {
			let rowData = $("#children_list").datagrid("getSelected");

			if (rowData) {
				let childrenId = rowData.id;

				$("#children_id").val(childrenId);
				$("#jiadian_name").html(rowData.name);
				$("#jiadian_grade").html(rowData.grade);

				ajaxGet("/children_category/selectById", {
					id: rowData.categoryId
				}, function(resp) {
					let data = resp.data;

					$("#jiadian_image").attr({
						alt: data.name,
						src: data.image,
						title: data.name
					});
				}, error);

				ajaxGet("/children_jiadian/selectByChildrenId", {
					childrenId: childrenId
				}, function(resp) {
					let data = resp.data;

					$("#jiadian_moli").numberspinner("setValue", data.moli);
					$("#jiadian_naili").numberspinner("setValue", data.naili);
					$("#jiadian_tizhi").numberspinner("setValue", data.tizhi);
					$("#jiadian_minjie").numberspinner("setValue", data.minjie);
					$("#jiadian_liliang").numberspinner("setValue", data.liliang);
				}, error);

				ajaxGet("/children_attribute/selectByChildrenId", {
					childrenId: childrenId
				}, function(resp) {
					let data = resp.data;

					$("#jiadian_qixue").numberspinner("setValue", data.qixue);
					$("#jiadian_wlsh").numberspinner("setValue", data.wlsh);
					$("#jiadian_fssh").numberspinner("setValue", data.fssh);
					$("#jiadian_wlfy").numberspinner("setValue", data.wlfy);
					$("#jiadian_fsfy").numberspinner("setValue", data.fsfy);
					$("#jiadian_sudu").numberspinner("setValue", data.sudu);
				}, error);

				$("#jiadian_dialog").dialog("open");
			} else {
				unselected();
			}
		}
	});

	/**
	 * 点击子女装备
	 */
	for (let i = 1; i < equipmentTypes.length; i++) {
		let equipmentType = equipmentTypes[i];

		$(equipmentType.selector).on("click", function() {
			clickEquipment($(equipmentType.selector), equipmentType.type);
		}).attr("title", equipmentType.name);
	}

	$("#tabs").tabs({
		fit: true,
		pill: true,
		border: false,
		narrow: false,
		justified: true,
		closable: false,
		showHeader: true
	});
	
	$("#skill_panel").panel({
		fit: true
	});

	$("#children_detail .splitter").textbox({
		width: 320
	});

	/**
	 * 健康度
	 */
	$("#health").progressbar({
		width: 220,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 亲密度
	 */
	$("#intimacy").progressbar({
		width: 220,
		text: "{value}"
	}).progressbar("setColor", "green");

	/**
	 * 子女详情对话框
	 */
	$("#children_detail").dialog({
		title: " "
	});

	/******************************************************************************************************/

	$("#more").linkbutton({
		text: "&emsp;&emsp;穿戴&emsp;&emsp;",
		onClick: function() {
			// todo
		}
	});

	$("#wear").linkbutton({
		text: "&emsp;&emsp;穿戴&emsp;&emsp;",
		onClick: function() {
			let rowData = $("#children_datalist").datalist("getSelected");

			if (rowData) {
				if (!equipmentId) {
					let equipment = $("#equipment_list").datalist("getSelected");

					if (equipment) {
						equipmentId = equipment.id;
					} else {
						alert("请选择要穿戴的装备");
					}
				}

				ajaxPost("/children_equipment/wear", {
					childrenId: rowData.id,
					equipmentId: equipmentId
				}, function(resp) {
					showMsg(resp.message);
					loadEquipments();

					$("#equipment_list").datalist("reload").datagrid("unselectAll");
					$("#equipment_detail").dialog("close");
				}, error);
			}
		}
	});

	$("#equipment_detail .splitter").textbox({
		width: 200
	});

	/**
	 * 子女装备详情对话框
	 */
	$("#equipment_detail").dialog({
		title: " ",
		width: 225,
		onClose: function () {
			prepare();
		}
	});

	/******************************************************************************************************/

	/**
	 * 子女装备数据列表
	 */
	$("#equipment_list").datalist({
		url: "/children_equipment/selectList",
		idField: "id",
		valueField: "id",
		textField: "id",
		width: 222,
		height: 310,
		textFormatter: function(value, rowData) {
			ajaxGet("/children_equipment_category/selectById", {
				id: rowData.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#equipment_" + rowData.id).attr({
					"src": data.image,
					"title": data.name
				});
			}, error);

			let skillId = rowData.skillId;

			if (skillId) {
				ajaxGet("/chongwu_skill_category/selectById", {
					id: skillId
				}, function (resp) {
					let data = resp.data;

					$("#skill_" + rowData.id).html(data.name);
				}, error);
			}

			return "<table>" +
				"<tr>" +
				"<td rowspan='2'><img height='50' id='equipment_" +  rowData.id + "' /></td>" +
				"<td>打造：<span>" + rowData.forger + "</span></td>" +
				"</tr>" +
				"<tr>" +
				"<td>技能：<span class='blue' id='skill_" + rowData.id + "'>无</span></td>" +
				"</tr>" +
				"</table>";
		},
		onSelect: function(rowIndex, rowData) { // 这个是选中表格事件
			if (rowData) {
				viewEquipment(rowData);
			}
		}
	});

	/**
	 * 子女装备对话框
	 */
	$("#equipment_dialog").dialog({
		title: "子女装备",
		onClose: function() {
			$("#equipment_detail").dialog("close");
		}
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#children_list").datagrid({
		url: "/children/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", {
			iconCls: "icon-set",
			text: "设置资质",
			handler: function() {
				zizhiHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "预览子女",
			handler: function() {
				viewHandler();
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "子女名字", align: "center", width: 160},
			{field: "grade", title: "子女等级", align: "center", width: 160},
			{field: "categoryId", title: "子女造型", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/children_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#children_" + rowIndex).attr({
							alt: data.name,
							src: data.image,
							title: data.name
						});
					}, error);

					return "<img height='" + size + "' id='children_" + rowIndex + "' />";
				}
			},
			{field: "improveTimes", title: "提升次数", align: "center", width: 160
				, formatter: function(value) {
					return value + "次";
				}
			},
			{field: "healthiness", title: "健康度", align: "center", width: 160},
			{field: "schoolId", title: "所属门派", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#school_" + rowIndex).attr({
							"title": data.name,
							"src": data.image
						});
					}, error);

					return "<img height='" + size + "' id='school_" + rowIndex + "' />";
				}
			},
			roleDetailsColumn
		]]
	});

});